<?php

/**
* manges http requests views
*/
class django_views extends wakService {

	/**
	* executes a view to serve http request
	* @access public
	* @param string $view class.method
	* @param django_httprequest $request
	* @return django_httpresponse
	*/
	public function runView($view,$request) {
		// searching for view
		// search for view file
		//echo $viewCall;
		$parsed=$this->_parseViewName($view);
		if (!$this->_searchViewClass($parsed['class'])) {
			//trigger_error("view $view not found");
			throw new Exception("view $view not found !");
			return false;
		}
		// calling view
		return $this->_callView($parsed['class'],$parsed['method'],$request);
		//print_r($urlconf);
		//echo $urlconf->view;
		//exit;
	}/**/
	
	/**
	* parses view calls
	* @access protected
	* @param string $view
	* @return array ('name','method');
	*/
	protected function _parseViewName($view) {
		$view=explode('.',$view);
		//$pkg=$view
		$view['class']=$view[0];
		$view['method']=$view[1];
		return $view;
	}/**/
	
	/**
	* searches for view class and include its def file
	* @access protected
	* @param string $class
	* @return bool
	*/
	protected function _searchViewClass($class) {
		if (class_exists($class))
			return true;
		$file='app/django/views/'.$class.'.class.php';
		if (!file_exists($file))
			return false;
		include($file);
		return class_exists($class);
	}/**/
	
	/**
	* calls view static class/method
	* @access protected
	* @param string $class
	* @param string $method
	* @param django_httprequest &$request
	* @return django_httpresponse
	*/
	protected function _callView($class,$method,&$request) {
		return call_user_func(array($class,$method),$request);
	}
}
?>
